﻿using AutoMapper;
using BackData;
using Domain;
using Infratructure;
using MediatR;
using Server.API.Application.Command.Kill;

namespace Server.API.Application.Handler.Kill
{
    public class UpdateKillHandler : IRequestHandler<UpdateKillCommand, APIResult<int>>
    {
        private readonly IRepository<KillModel> killway;
        private readonly ILogger<UpdateKillHandler> log;
        private readonly IMapper mapper;

        public UpdateKillHandler(IRepository<KillModel> killway, ILogger<UpdateKillHandler> log, IMapper mapper)
        {
            this.killway = killway;
            this.log = log;
            this.mapper = mapper;
        }
        /// <summary>
        /// 修改屠宰信息
        /// </summary>
        /// <param name="request">参数</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>影响行数</returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<int>> Handle(UpdateKillCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> res = new APIResult<int>();
            var value = mapper.Map<KillModel>(request);
            var result = await killway.Updateway(value);
            if (result > 0)
            {
                log.LogInformation("屠宰修改成功");
                res.Num = StateCode.OK;
                res.Message = "修改成功";
                res.Data = result;
            }
            else
            {
                log.LogError("屠宰修改失败");
                res.Num = StateCode.Fail;
                res.Message = "修改失败";
            }
            return res;
        }
    }
}
